Quarto’s additions to Pandoc’s Markdown

last modified

2023–9–3

Executable code and cell options

In Quarto documents, code blocks marked with {python} (or another supported language) are included as standard syntax-highlighted code blocks, but they are also executed and their output included after the code itself.

What is the sum of two and two?

```{python}
2 + 2
```

It is four!

What is the sum of two and two?

2 + 2
4

It is four!

Text output is shown in a non-highlighted code block.

Errors and warnings can be captured and the messages displayed using the cell options error and warning:

```{python}
#| error: true
1 / 0
```
1 / 0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[2], line 1
----> 1 1 / 0

ZeroDivisionError: division by zero

One can also create document text in Markdown (or HTML) format from code (cell option output: asis), in which case it makes sense not to show the code (cell option echo: false):

```{python}
#| output: asis
#| echo: false
a = 2
b = 3
print(f'The *sum* of {a} and {b} is {a + b}, their *product*  is {a * b}.')
```

The sum of 2 and 3 is 5, their product is 6.

Many cell options can also be set as document format options.

Rich code output

Rich output created by code using the mechanisms of Jupyter can also be embedded, for example a table

```{python}
import pandas as pd

df = pd.DataFrame({'a': [2, 1, 3], 'b': [5, 7, 6]})
df
```
import pandas as pd

df = pd.DataFrame({'a': [2, 1, 3], 'b': [5, 7, 6]})
df
a b
0 2 5
1 1 7
2 3 6

or a plot:

```{python}
#| fig-cap: Matplotlib plot from data frame `df`.
df.plot();
```
df.plot();

Matplotlib plot from data frame df.

Here, fig-cap is a cell option which adds a caption to the figure.

Pandas’ plotting uses by default Matplotlib, but for interactive figures one can also use the Bokeh backend:

```{python}
#| fig-cap: Bokeh plot from data frame `df`.
import pandas_bokeh
pandas_bokeh.output_notebook()
pd.set_option('plotting.backend', 'pandas_bokeh')
df.plot();
```
#| fig-cap: Bokeh plot from data frame `df`.
import pandas_bokeh
pandas_bokeh.output_notebook()
pd.set_option('plotting.backend', 'pandas_bokeh')
df.plot();

or the Plotly backend:

```{python}
#| fig-cap: Plotly plot from data frame `df`.
import plotly
pd.set_option('plotting.backend', 'plotly')
df.plot(height=400, width=630)
```
import plotly
pd.set_option('plotting.backend', 'plotly')
df.plot(height=400, width=630)

Plotly plot from data frame df.

The fig-cap cell option is not properly supported for either.